﻿package core.ui.controls
{
    import core.event.*;
    import flash.display.*;
    import flash.text.*;

    public class Slidebar extends Sprite
    {
        var decimal:Boolean;
        var min:Number;
        var max:Number;
        var topic:Literal;
        var tb:NumberInput;
        var slider:Slider;
        var disable:Sprite;

        public function Slidebar(param1:String, param2:Number, param3:Number, param4:int, param5:Number = 100, param6:Boolean = false, param7:Boolean = false, param8:int = 40)
        {
            this.min = param2;
            this.max = param3;
            this.decimal = param7;
            this.slider = new Slider(param4, Math.round((param5 - param2) / (param3 - param2) * 100));
            this.slider.addEventListener(SliderEvent.DRAG, this.SliderDrag, false, 1, true);
            this.slider.addEventListener(SliderEvent.RELEASE, this.SliderRelease, false, 1, true);
            addChild(this.slider);
            this.topic = new Literal(param1);
            addChild(this.topic);
            this.tb = new NumberInput(param8, param5, param2, param3, param7, true, 18);
            this.tb.addEventListener(NumberEvent.CHANGE, this.TextValueChange, false, 1, true);
            addChild(this.tb);
            if (param6)
            {
                this.slider.y = -1;
                this.topic.x = -this.topic.width - 2;
                this.topic.y = 2;
                this.tb.x = param4 + 7;
                this.tb.y = 1;
            }
            else
            {
                this.slider.y = 15;
                this.topic.x = -2;
                this.tb.x = param4 - this.tb.width - 2;
                this.tb.y = -1;
            }
            return;
        }// end function

        public function Disable(disable:Boolean = true) : void
        {
            if (this.disable == null)
            {
                this.disable = new Sprite();
                this.disable.visible = false;
                this.disable.alpha = 0;
                this.disable.graphics.beginFill(9474192);
                this.disable.graphics.drawRect(0, 0, 10, 10);
                this.disable.graphics.endFill();
                this.disable.height = this.height;
                this.disable.width = this.width;
                addChild(this.disable);
            }
            this.tb.t.type = disable ? (TextFieldType.DYNAMIC) : (TextFieldType.INPUT);
            this.alpha = disable ? (0.4) : (1);
            this.disable.visible = disable;
            return;
        }// end function

        public function SliderDrag(event:SliderEvent) : void
        {
            var _loc_2:Number = NaN;
            if (!event.manual)
            {
                _loc_2 = this.tb.value;
                this.tb.value = event.percent * (this.max - this.min) + this.min;
                if (this.tb.value != _loc_2)
                {
                    dispatchEvent(new SliderEvent(SliderEvent.DRAG));
                }
            }
            return;
        }// end function

        public function SliderRelease(event:SliderEvent) : void
        {
            dispatchEvent(new SliderEvent(SliderEvent.CHANGE, this.value));
            return;
        }// end function

        public function TextValueChange(event:NumberEvent) : void
        {
            this.slider.Set((event.val - this.min) / (this.max - this.min));
            dispatchEvent(new SliderEvent(SliderEvent.CHANGE));
            dispatchEvent(new SliderEvent(SliderEvent.DRAG));
            return;
        }// end function

        public function Set(disable:Number, disable:Boolean = false) : void
        {
            this.slider.Set((disable - this.min) / (this.max - this.min));
            this.tb.value = disable;
            return;
        }// end function

        public function get value() : Number
        {
            return this.tb.value;
        }// end function

    }
}
